/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class TypetaxeJpaController implements Serializable {

    public TypetaxeJpaController() {
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Typetaxe typetaxe) {
        if (typetaxe.getTaxeCollection() == null) {
            typetaxe.setTaxeCollection(new ArrayList<Taxe>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Taxe> attachedTaxeCollection = new ArrayList<Taxe>();
            for (Taxe taxeCollectionTaxeToAttach : typetaxe.getTaxeCollection()) {
                taxeCollectionTaxeToAttach = em.getReference(taxeCollectionTaxeToAttach.getClass(), taxeCollectionTaxeToAttach.getIdTaxe());
                attachedTaxeCollection.add(taxeCollectionTaxeToAttach);
            }
            typetaxe.setTaxeCollection(attachedTaxeCollection);
            em.persist(typetaxe);
            for (Taxe taxeCollectionTaxe : typetaxe.getTaxeCollection()) {
                Typetaxe oldIdTypetaxeOfTaxeCollectionTaxe = taxeCollectionTaxe.getIdTypetaxe();
                taxeCollectionTaxe.setIdTypetaxe(typetaxe);
                taxeCollectionTaxe = em.merge(taxeCollectionTaxe);
                if (oldIdTypetaxeOfTaxeCollectionTaxe != null) {
                    oldIdTypetaxeOfTaxeCollectionTaxe.getTaxeCollection().remove(taxeCollectionTaxe);
                    oldIdTypetaxeOfTaxeCollectionTaxe = em.merge(oldIdTypetaxeOfTaxeCollectionTaxe);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Typetaxe typetaxe) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typetaxe persistentTypetaxe = em.find(Typetaxe.class, typetaxe.getIdTypetaxe());
            Collection<Taxe> taxeCollectionOld = persistentTypetaxe.getTaxeCollection();
            Collection<Taxe> taxeCollectionNew = typetaxe.getTaxeCollection();
            List<String> illegalOrphanMessages = null;
            for (Taxe taxeCollectionOldTaxe : taxeCollectionOld) {
                if (!taxeCollectionNew.contains(taxeCollectionOldTaxe)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Taxe " + taxeCollectionOldTaxe + " since its idTypetaxe field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Taxe> attachedTaxeCollectionNew = new ArrayList<Taxe>();
            for (Taxe taxeCollectionNewTaxeToAttach : taxeCollectionNew) {
                taxeCollectionNewTaxeToAttach = em.getReference(taxeCollectionNewTaxeToAttach.getClass(), taxeCollectionNewTaxeToAttach.getIdTaxe());
                attachedTaxeCollectionNew.add(taxeCollectionNewTaxeToAttach);
            }
            taxeCollectionNew = attachedTaxeCollectionNew;
            typetaxe.setTaxeCollection(taxeCollectionNew);
            typetaxe = em.merge(typetaxe);
            for (Taxe taxeCollectionNewTaxe : taxeCollectionNew) {
                if (!taxeCollectionOld.contains(taxeCollectionNewTaxe)) {
                    Typetaxe oldIdTypetaxeOfTaxeCollectionNewTaxe = taxeCollectionNewTaxe.getIdTypetaxe();
                    taxeCollectionNewTaxe.setIdTypetaxe(typetaxe);
                    taxeCollectionNewTaxe = em.merge(taxeCollectionNewTaxe);
                    if (oldIdTypetaxeOfTaxeCollectionNewTaxe != null && !oldIdTypetaxeOfTaxeCollectionNewTaxe.equals(typetaxe)) {
                        oldIdTypetaxeOfTaxeCollectionNewTaxe.getTaxeCollection().remove(taxeCollectionNewTaxe);
                        oldIdTypetaxeOfTaxeCollectionNewTaxe = em.merge(oldIdTypetaxeOfTaxeCollectionNewTaxe);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = typetaxe.getIdTypetaxe();
                if (findTypetaxe(id) == null) {
                    throw new NonexistentEntityException("The typetaxe with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typetaxe typetaxe;
            try {
                typetaxe = em.getReference(Typetaxe.class, id);
                typetaxe.getIdTypetaxe();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The typetaxe with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Taxe> taxeCollectionOrphanCheck = typetaxe.getTaxeCollection();
            for (Taxe taxeCollectionOrphanCheckTaxe : taxeCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Typetaxe (" + typetaxe + ") cannot be destroyed since the Taxe " + taxeCollectionOrphanCheckTaxe + " in its taxeCollection field has a non-nullable idTypetaxe field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(typetaxe);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Typetaxe> findTypetaxeEntities() {
        return findTypetaxeEntities(true, -1, -1);
    }

    public List<Typetaxe> findTypetaxeEntities(int maxResults, int firstResult) {
        return findTypetaxeEntities(false, maxResults, firstResult);
    }

    private List<Typetaxe> findTypetaxeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Typetaxe.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Typetaxe findTypetaxe(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Typetaxe.class, id);
        } finally {
            em.close();
        }
    }

    public int getTypetaxeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Typetaxe> rt = cq.from(Typetaxe.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
